package com.mawujun.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import sun.misc.BASE64Decoder;

public class Encrypt {
	
	public static void main(String[] parms) {
		//http://topic.csdn.net/u/20071124/16/1f8843b9-4864-497e-8759-df094459a8cc.html
		//123456 編碼的記過是fEqNCco3Yq9h5ZUglD3CZJT4lBs=
		//123456 用SHA1编码得出的结果是:7C4A8D09CA3762AF61E59520943DC26494F8941B

		Encrypt aa=new Encrypt();
		try {
			
			System.out.println(aa.getBASE64(aa.SHA1ToHax("1"))+""); //這個是正確的，哇哈哈
			System.out.println(aa.getBASE64(aa.SHA1ToHax("abcdefg"))+""); //這個是正確的，哇哈哈
			System.out.println(aa.getBASE64(aa.SHA1ToHax("ABCDEFG"))+""); 
			System.out.println(aa.getBASE64(aa.SHA1ToHax("zkz123f"))+""); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 public String MD5(String s)
	    throws Exception
	  {
	    String s1 = "";
	    if(s.trim() == null)
	        return "null";
	    try
	    {
	        s1 = System.getProperty("MD5.algorithm", "MD5");
	    }
	    catch(SecurityException _ex) { }
	    MessageDigest messagedigest = MessageDigest.getInstance(s1);
	    byte abyte0[] = s.getBytes();
	    for(int i = 0; i < s.length(); i++)
	        messagedigest.update(abyte0, 0, i);

	    byte abyte1[] = messagedigest.digest();
	    BigInteger biginteger = new BigInteger(abyte1);
	    return biginteger.toString(16);
	  }

	  public String SHA(String s)
	    throws Exception
	  {
	    String s1 = "";
	    if(s.trim() == null)
	        return "null";
	    try
	    {
	        s1 = System.getProperty("SHA.algorithm", "SHA");
	    }
	    catch(SecurityException _ex) { }
	    MessageDigest messagedigest = MessageDigest.getInstance(s1);
	    byte abyte0[] = s.getBytes();
	    messagedigest.update(abyte0);
	    byte abyte1[] = messagedigest.digest();
	    BigInteger biginteger = new BigInteger(abyte1);
	    return biginteger.toString(32);
	  }
	  
	// 将 s 进行 BASE64 编码 
	  public  String getBASE64(String s) { 
		  if (s == null) return null; 
		  return Base64.encode( s.getBytes() ); 
	  } 
	  public  String getBASE64(byte[] s) { 
		  if (s == null) return null; 
		  return Base64.encode( s ); 
	  } 

	  // 将 BASE64 编码的字符串 s 进行解码 
	  public  String getFromBASE64(String s) { 
		  if (s == null) return null; 
		  BASE64Decoder decoder = new BASE64Decoder(); 
		  try { 
		  byte[] b = decoder.decodeBuffer(s); 
		  return new String(b); 
		  } catch (Exception e) { 
		  return null; 
		  } 
	  }
	  
	  public String SHA1(String inStr) {
	        MessageDigest md = null;
	        
	        //加密后的字符串
			String strDes = null;
			
	        byte[] bt = inStr.getBytes();
	        try {
	            md = MessageDigest.getInstance("SHA-1");     //选择SHA-1，也可以选择MD5
	            md.update(bt);
	            //通过执行诸如填充之类的最终操作完成哈希计算
	    		strDes = bytes2Hex(md.digest()); //to HexString
	        }
	        catch (NoSuchAlgorithmException nsae) {
	            nsae.printStackTrace();
	        }
	        return strDes;
	    }
	//将字节数组转换成16进制的字符串
		private String bytes2Hex(byte[] bts) {
		String des = "";
		String tmp = null;

		for (int i = 0; i < bts.length; i++) {
		tmp = (Integer.toHexString(bts[i] & 0xFF));
		if (tmp.length() == 1) {
		des += "0";
		}
		des += tmp;
		}
		return des;
		}
	    
		public byte[] SHA1ToHax(String inStr) {
	        MessageDigest md = null;
	        
	        //加密后的字符串
	        byte[] strDes = null;
			
	        byte[] bt = inStr.getBytes();
	        try {
	            md = MessageDigest.getInstance("SHA-1");     //选择SHA-1，也可以选择MD5
	            md.update(bt);
	            //通过执行诸如填充之类的最终操作完成哈希计算
	    		strDes = md.digest();
	        }
	        catch (NoSuchAlgorithmException nsae) {
	            nsae.printStackTrace();
	        }
	        return strDes;
	    }



}
